package com.xxx.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.reggie.common.CustomException;
import com.xxx.reggie.dto.SetmealDto;
import com.xxx.reggie.entity.DishFlavor;
import com.xxx.reggie.entity.Setmeal;
import com.xxx.reggie.entity.SetmealDish;
import com.xxx.reggie.mapper.SetmealMapper;
import com.xxx.reggie.service.DishService;
import com.xxx.reggie.service.SetmealDishService;
import com.xxx.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 新增套餐,同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto){
        //保存setmeal数据
        this.save(setmealDto);
        //保存setmeal_dish数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //为每一条数据赋值,赋值setmealid
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存数据
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id删除setmeal数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //删除setmeal数据
        //查询是否含有满足删除条件的数据
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        if (count > 0){
            throw new CustomException("套餐正在售卖中,不能删除");
        }
        this.removeByIds(ids);

        //删除setmealdish数据
        //条件查询器
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表的数据---setmealdish
        setmealDishService.remove(lambdaQueryWrapper);



    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDto getWithDish(Long id) {
        //查询setmeal数据进行存储到SetmealDto中
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //获取对应的setmealdish的属性值进行回显
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //添加数据到对象中
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新setmeal数据
        this.updateById(setmealDto);
        //删除setmealdish对象的重复数据,在进行数据的加载
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //删除
       setmealDishService.remove(queryWrapper);

       //添加对应的数据
        List<SetmealDish> setmealDishes= setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }


}
